{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FAISS not found. Attempting to install...\n",
      "GPU is not available. Installing faiss-cpu...\n",
      "Collecting faiss-cpu\n",
      "  Downloading faiss_cpu-1.10.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (4.4 kB)\n",
      "Collecting numpy<3.0,>=1.25.0 (from faiss-cpu)\n",
      "  Downloading numpy-2.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)\n",
      "Requirement already satisfied: packaging in ./.conda/lib/python3.11/site-packages (from faiss-cpu) (24.2)\n",
      "Downloading faiss_cpu-1.10.0-cp311-cp311-manylinux_2_28_x86_64.whl (30.7 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m30.7/30.7 MB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hDownloading numpy-2.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.4 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m16.4/16.4 MB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
      "\u001b[?25hInstalling collected packages: numpy, faiss-cpu\n",
      "Successfully installed faiss-cpu-1.10.0 numpy-2.2.2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FAISS imported successfully.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import subprocess\n",
    "import importlib\n",
    "\n",
    "def install_faiss():\n",
    "    try:\n",
    "        # 检查是否有 GPU 支持\n",
    "        gpu_available = False\n",
    "        try:\n",
    "            subprocess.check_output([\"nvidia-smi\"])\n",
    "            gpu_available = True\n",
    "        except (FileNotFoundError, subprocess.CalledProcessError):\n",
    "            gpu_available = False\n",
    "\n",
    "        # 根据 GPU 是否可用，安装对应的 FAISS 版本\n",
    "        if gpu_available:\n",
    "            print(\"GPU is available. Installing faiss-gpu...\")\n",
    "            subprocess.run([\"pip\", \"install\", \"faiss-gpu\"])\n",
    "        else:\n",
    "            print(\"GPU is not available. Installing faiss-cpu...\")\n",
    "            subprocess.run([\"pip\", \"install\", \"faiss-cpu\"])\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Failed to install FAISS: {e}\")\n",
    "\n",
    "def import_faiss():\n",
    "    try:\n",
    "        import faiss\n",
    "        print(\"FAISS imported successfully.\")\n",
    "        return faiss\n",
    "    except ImportError:\n",
    "        print(\"FAISS not found. Attempting to install...\")\n",
    "        install_faiss()\n",
    "        return import_faiss()\n",
    "\n",
    "# 动态导入 FAISS\n",
    "faiss = import_faiss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting numpy==1.23.5 (from -r requirements.txt (line 1))\n",
      "  Using cached numpy-1.23.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.3 kB)\n",
      "\u001b[31mERROR: Could not find a version that satisfies the requirement faiss_gpu==1.7.2 (from versions: none)\u001b[0m\u001b[31m\n",
      "\u001b[0m\u001b[31mERROR: No matching distribution found for faiss_gpu==1.7.2\u001b[0m\u001b[31m\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'tiktoken'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[10], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m get_ipython()\u001b[38;5;241m.\u001b[39msystem(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpip install -r requirements.txt\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msrc\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01magents\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01moutline_writer\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m outlineWriter\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msrc\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdatabase\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m PaperDatabase\n",
      "File \u001b[0;32m/mnt/c/project/ai_create_survey_work/src/agents/outline_writer.py:3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnumpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mtiktoken\u001b[39;00m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mtqdm\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m trange,tqdm\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mtime\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'tiktoken'"
     ]
    }
   ],
   "source": [
    "!pip install -r requirements.txt\n",
    "from src.agents.outline_writer import outlineWriter\n",
    "from src.database import PaperDatabase\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.agents.outline_writer import outlineWriter\n",
    "from src.database import PaperDatabase\n",
    "'''\n",
    "# 创建数据库实例\n",
    "db = PaperDatabase(\n",
    "    db_type=\"local\",\n",
    "    local_config={\n",
    "        \"db_path\": \"./database\",\n",
    "        \"embedding_model\": \"./embedding_models/AI-ModelScope/nomic-embed-text-v1\",\n",
    "        \"device\": \"cuda\"  # 或 \"cpu\"\n",
    "    }\n",
    ")\n",
    "'''\n",
    "\n",
    "# 创建数据库实例\n",
    "db = PaperDatabase(\n",
    "    db_type=\"atomgit\",\n",
    "    atomgit_config={\n",
    "        \"base_url\": \"http://180.184.65.98:38880/atomgit\"\n",
    "    }\n",
    ")\n",
    "\n",
    "\n",
    "outline_writer = outlineWriter(model='deepseek-chat', api_key=\"sk-092e43fc568543379eca774f4d4357a8\", api_url=\"https://api.deepseek.com\", database=db)\n",
    "\n",
    "outline = outline_writer.api_model.chat('hello')\n",
    "\n",
    "#outline = outline_writer.draft_outline(topic='小儿疝气治疗，尤其突出保守治疗',reference_num = 300)\n",
    "print(outline)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = database(db_path='./database', embedding_model=\"./embedding_models/AI-ModelScope/nomic-embed-text-v1/\")\n",
    "outline_writer = outlineWriter(model='deepseek-chat', api_key=\"sk-092e43fc568543379eca774f4d4357a8\", api_url=\"https://api.deepseek.com\", database=db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "outline_writer.api_model.chat('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "outline = outline_writer.draft_outline('慢性鼻窦炎的药物研究进展')\n",
    "print(outline)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
